//
//  MessageView.m
//  mendianjieke
//
//  Created by 响当当 on 8/26/19.
//  Copyright © 2019 dongzheng. All rights reserved.
//

#import "MessageView.h"

@interface MessageView()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)UIView *voiceView;
@property (nonatomic, strong)UIView *shakeView;

@end

@implementation MessageView

- (UIView *)voiceView
{
    if (!_voiceView)
    {
        _voiceView = [[UIView alloc]initWithFrame:CGRectMake(0,5*kUIScaleBasePlus,SCREEN_WIDTH,50*kUIScaleBasePlus)];
        _voiceView.backgroundColor = [UIColor whiteColor];
        
        UILabel *titleLabel = [Utilties creatLabelWithFrame:CGRectMake(15*kUIScaleBasePlus,15*kUIScaleBasePlus,120*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentLeft
                                                   textClor:LCHexColor(0x282828)
                                                   textFont:14*kUIScaleBasePlus];
        titleLabel.text = @"声音";
        [_voiceView addSubview:titleLabel];
        
        UISwitch *voiceSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-34*kUIScaleBasePlus-15*kUIScaleBasePlus,10*kUIScaleBasePlus,34*kUIScaleBasePlus,21*kUIScaleBasePlus)];
        voiceSwitch.transform = CGAffineTransformMakeScale(0.7,0.7);
        NSString *voice = LCUserDefaultsGet(@"Voice");
        if ([voice isEqualToString:@"1"])
        {
            voiceSwitch.on = YES;
        }
        else
        {
            voiceSwitch.on = NO;
        }
        voiceSwitch.tintColor = LCHexColor(0xC3C3C3);
        [voiceSwitch addTarget:self action:@selector(voiceChanged:) forControlEvents:(UIControlEventValueChanged)];
        [_voiceView addSubview:voiceSwitch];
        
    }
    return _voiceView;
}

- (UIView *)shakeView
{
    if (!_shakeView)
    {
        _shakeView = [[UIView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.voiceView.frame)+5*kUIScaleBasePlus,SCREEN_WIDTH,50*kUIScaleBasePlus)];
        _shakeView.backgroundColor = [UIColor whiteColor];

        
        UILabel *titleLabel = [Utilties creatLabelWithFrame:CGRectMake(15*kUIScaleBasePlus,15*kUIScaleBasePlus,120*kUIScaleBasePlus,20*kUIScaleBasePlus)
                                               textAligment:NSTextAlignmentLeft
                                                   textClor:LCHexColor(0x282828)
                                                   textFont:14*kUIScaleBasePlus];
        titleLabel.text = @"震动";
        [_shakeView addSubview:titleLabel];
        
        UISwitch *shakeSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-34*kUIScaleBasePlus-15*kUIScaleBasePlus,10*kUIScaleBasePlus,34*kUIScaleBasePlus,21*kUIScaleBasePlus)];
        shakeSwitch.transform = CGAffineTransformMakeScale(0.7,0.7);
        NSString *shake = LCUserDefaultsGet(@"Shake");
        if ([shake isEqualToString:@"1"])
        {
            shakeSwitch.on = YES;
        }
        else
        {
            shakeSwitch.on = NO;
        }
        shakeSwitch.tintColor = LCHexColor(0xC3C3C3);
        [shakeSwitch addTarget:self action:@selector(shakeChanged:) forControlEvents:(UIControlEventValueChanged)];
        [_shakeView addSubview:shakeSwitch];

    }
    return _shakeView;
}


- (UITableView *)tableView
{
    if (!_tableView)
    {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = 0;
        _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
        _tableView.showsVerticalScrollIndicator = NO;
    }
    return _tableView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor groupTableViewBackgroundColor];
        [self addSubview:self.voiceView];
        [self addSubview:self.shakeView];
    }
    return self;
}

- (void)voiceChanged:(UISwitch *)sender
{
    BOOL isVoiceOn = [sender isOn];

    if (isVoiceOn)
    {
        LCUserDefaultsSet(@"1", @"Voice");
        [VOICE play];
    }
    else
    {
        LCUserDefaultsSet(@"0", @"Voice");
    }
    LCUserDefaultsSyn;

}

- (void)shakeChanged:(UISwitch *)sender
{
    BOOL isShakeOn = [sender isOn];
    
    if (isShakeOn)
    {
        LCUserDefaultsSet(@"1", @"Shake");
        [SHAKER play];
    }
    else
    {
        LCUserDefaultsSet(@"0", @"Shake");
    }
    LCUserDefaultsSyn;
}
@end
